const isFunction = variable => typeof variable === 'function'
const PENDING = 'PENDING'
const FULFILLED = 'FULFILLED'
const REJECTED = 'REJECTED'
class myPromise {
  constructor(handle) {
    if(!isFunction(handle)) {
      return new Error('参数必须为函数')
    }
    this._status = PENDING
    this._value = null
    this.fulfilledQueen = []
    this.rejectedQueen = []
    handle(this._resolve.bind(this), this._reject.bind(this))
  }
  // 添加resovle时执行的函数
  _resolve (val) {
    if (this._status !== PENDING) return
    this._status = FULFILLED
    this._value = val
  }
  // 添加reject时执行的函数
  _reject (err) { 
    if (this._status !== PENDING) return
    this._status = REJECTED
    this._value = err
  }
  // then方法
  _then(onFulfilled,onReject) {
    let { _status, _value } = this
    switch (_status) {
      case PENDING :
        this.fulfilledQueen.push(onFulfilled)
        this.fulfilledQueen.push(onReject)
        break
      case FULFILLED :
        onFulfilled(_value)
        break
      case FULFILLED :
        onReject(_value)
        break
    }
    return new myPromise((onFulfillednext,onRejectnext) => {})
  }
  // catch方法
  _catch(onReject) {
    return this._then(undefined,onReject)
  }
  // all方法
  static _all(list) {
    return new myPromise((resolve, reject) => {
      let _value = []
      let num = 0
      for(let [i, p] of list.entries()) {
        this.resolve(p).then(res => {
          _value[i] = res
          num++
          if(num === list.length) resolve(values)
        }, err => { reject(err) })
      }
    })
  }
}